<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="20">
                <el-button  @click="goFlashClass" type="primary">
                            开始刷课
                </el-button>
                <el-button  @click="stopFlashClass" type="primary">
                            停止刷课
                </el-button>
                <el-button  @click="lookLog" type="primary">
                            查看日志
                </el-button>
                <el-affix :offset="50">
                    <span style="color:red;">当前刷课信息(5秒更新一次):{{ infoFlash }}</span>
                    
                </el-affix>
            </el-col>
            <el-col :span="12" v-for="item in infoList" >
                <el-card style="max-width: 680px">
                    <template #header>
                    <div class="card-header">
                        <span>{{ item.title }}</span>
                    </div>
                    </template>
                    <p  class="text item">当前进度: {{ item.progress_time }}</p>
                    <p  class="text item">总前进度: {{ item.total_time }}</p>
                    
                    <template #footer>
                        完成状态: 
                        <el-tag type="danger" v-show="item.progress_time !== item.total_time">未完成</el-tag>
                        <el-tag type="success" v-show="item.progress_time == item.total_time">已刷完</el-tag>
                    </template>
                </el-card>
            </el-col>
        </el-row>
    </div>

</template>
<script setup lang="ts">
import {getAllClassInfo, flashClass, stopflashClass, getNewClassInfo, getFlashTime} from "@/axios/index"
import { ElMessage } from 'element-plus'

let intervalTimer:any = null;
let getFlashTimeintervalTimer:any = null;
let infoFlash = ref("")

onMounted(()=>{
        const sysInfo:string|null = localStorage.getItem('sysInfo') || null
        const userInfo:string|null = localStorage.getItem('userInfo') || null
        if(sysInfo!==null && userInfo!==null){
            const pSysInfo = JSON.parse(sysInfo)
            const pUserInfo = JSON.parse(userInfo)
            
            var classInfo = {
                idCar : pSysInfo['idCar'],
                userId : pUserInfo['id'],
            }
            allClassInfo(classInfo)
            intervalTimer = setInterval(()=>{
                allClassInfo(classInfo)
            }, 60000) // 一分钟刷新一次请求
        }else{
            ElMessage.error("请先去用户绑定刷课平台信息或检查登录信息是否正确")
        }
})
onBeforeUnmount(() => {
    if (intervalTimer) {
        clearInterval(intervalTimer);
        intervalTimer = null;
    }
    if(getFlashTimeintervalTimer){
        clearInterval(getFlashTimeintervalTimer);
        getFlashTimeintervalTimer = null;
    }
});
const infoList:any = ref([])

const goFlashClass = async ()=>{
    const sysInfo:string|null = localStorage.getItem('sysInfo') || null
    const userInfo:string|null = localStorage.getItem('userInfo') || null
    if(sysInfo!==null && userInfo!==null){
        const pSysInfo = JSON.parse(sysInfo)
        const pUserInfo = JSON.parse(userInfo)
        var flashInfo = {
            idCar:pSysInfo['idCar'],
            password:pSysInfo['password'],
            token:pSysInfo['token'],
            id:pUserInfo['id'],
        }
         const {data} = await flashClass(flashInfo)
         ElMessage.success(data)
    }else{
        ElMessage.error("请重新绑定刷课信息")
    }
}
const allClassInfo = async (pSysInfo:any)=>{
    const {data} = await getAllClassInfo(pSysInfo)
    if(data.status === 200){
        infoList.value = data.data.data.list
    }
}

const stopFlashClass = async ()=>{
    const sysInfo:string|null = localStorage.getItem('sysInfo') || null
    const userInfo:string|null = localStorage.getItem('userInfo') || null
    if(sysInfo!==null && userInfo!==null){
        const pSysInfo = JSON.parse(sysInfo)
        const pUserInfo = JSON.parse(userInfo)
        var flashInfo = {
            idCar:pSysInfo['idCar'],
            password:pSysInfo['password'],
            token:pSysInfo['token'],
            id:pUserInfo['id'],
        }
         const {data} = await stopflashClass(flashInfo)
         if(getFlashTimeintervalTimer){
                clearInterval(getFlashTimeintervalTimer);
                getFlashTimeintervalTimer = null;
            }
         ElMessage.success(data)
    }else{
        ElMessage.error("请重新绑定刷课信息")
    }
}


const lookLog = ()=>{
    const sysInfo:string|null = localStorage.getItem('sysInfo') || null
    const userInfo:string|null = localStorage.getItem('userInfo') || null
    if(sysInfo!==null && userInfo!==null){
        const pSysInfo = JSON.parse(sysInfo)
        const pUserInfo = JSON.parse(userInfo)
        var flashInfo = {
            idCar:pSysInfo['idCar'],
            password:pSysInfo['password'],
            token:pSysInfo['token'],
            id:pUserInfo['id'],
        }
         getFlashTimeintervalTimer = setInterval( async () => {
            const {data} = await getFlashTime(flashInfo)
            infoFlash.value = data
         }, 1000);
    }else{
        ElMessage.error("请重新绑定刷课信息")
    }
}

</script>

